/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 *
 * web http://sixrevisions.com/tutorials/javascript_tutorial/create-a-slick-and-accessible-slideshow-using-jquery/
 */
(function($){
$(document).ready(function(){
  var currentPositionN = 0;
  var slideWidthN = 200;
  var slidesN = $('.slideNews');
  var numberOfSlides = slidesN.length;

  // Remove scrollbar in JS
  $('#slidesContainerNews').css('overflow', 'hidden');

  // Wrap all .slides with #slideInner div
  slidesN
  .wrapAll('<div id="slideInnerNews"></div>')
  // Float left to display horizontally, readjust .slides width
  .css({
    'float' : 'left',
    'width' : slideWidthN
  });

  // Set #slideInner width equal to total width of all slides
  $('#slideInnerNews').css('width', slideWidthN * numberOfSlides);

  // Insert left and right arrow controls in the DOM
  $('#slideshowNews')
    .prepend('<span class="controlNews" id="leftControlNews">Move left</span>')
    .append('<span class="controlNews" id="rightControlNews">Move right</span>');

  // Hide left arrow control on first load
  manageControls(currentPositionN);

  // Create event listeners for .controls clicks
  $('.controlNews')
    .bind('click', function(){
    // Determine new position
      currentPositionN = ($(this).attr('id')=='rightControlNews')
    ? currentPositionN+1 : currentPositionN-1;

      // Hide / show controls
      manageControls(currentPositionN);
      // Move slideInner using margin-left
      $('#slideInnerNews').animate({
        'marginLeft' : slideWidthN*(-currentPositionN)
      });
    });

  // manageControls: Hides and shows controls depending on currentPosition
  function manageControls(position){
    // Hide left arrow if position is first slide
    if(position==0){ $('#leftControlNews').hide() }
    else{ $('#leftControlNews').show() }
    // Hide right arrow if position is last slide
    if(position==numberOfSlides-1){ $('#rightControlNews').hide() }
    else{ $('#rightControlNews').show() }
    }
  });
})(jQuery);